/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { XButton } = require("../engine/control/XButton");
const { X2DBeta } = require("../engine/graphics/X2DInterface");
const { Scr } = require("../engine/XDefine");
const { XTools } = require("../engine/XTools");
const { XStat } = require("../engine/XStat");
const { XWindow } = require("../engine/control/XWindow");
const { XInput } = require("../engine/control/XInput");
const { WsClient2 } = require("../engine/ws/WsClient2");
const { PackageTool } = require("./ros_msg_utils/PackageTool");
const { XMultlineText } = require("../engine/graphics/XMultlineText");
const { Page3SelectPrj } = require("./Page3SelectPrj");
const { ProjectConfig } = require("./ros1/ProjectConfig");

class Page2SelectEnv extends XWindow {
    constructor() {
        super("Page2SelectEnv", false);

        this.env_ = {
            "hostDesc": "getting...",
            "ros1": false,
            "ros2": false,
            "dora": false,
            "userDir": "getting...",
        }
        WsClient2.registProcess("get_env_ret", (pt_ret) => {
            this.env_.hostDesc = pt_ret.get_string();
            this.env_.ros1 = pt_ret.get_bool();
            this.env_.ros2 = pt_ret.get_bool();
            this.env_.dora = pt_ret.get_bool();
            this.env_.userDir = pt_ret.get_string();
            console.log(this.env_);
        })
        let pt = new PackageTool();
        pt.put_string("get_env");
        pt.put_bool(XTools.ENABLE_COMPRESS);
        WsClient2.send(pt, false);

        this.btn_ros1 = new XButton({
            name: "ROS1",
            x: 100,
            y: Scr.logich / 2 + 50,
            w: 100,
            h: 100,
            onClicked: () => {
                G.ROS_NAME = "ROS1";
                ProjectConfig.WORKSPACE_PATH = this.env_.userDir + "/ros_workspace/ros1";//data/ros_workspace
                if (this.env_.ros1) {//已安装ros1,进入创建工程页面
                    XStat.popStat();
                    Page3SelectPrj.open();
                }
            },
            onDraw: () => {
                this.drawButton(this.btn_ros1, null, this.env_.ros1);
            }
        });
        this.btn_ros2 = new XButton({
            name: "ROS2",
            x: 300,
            y: Scr.logich / 2 + 50,
            w: 100,
            h: 100,
            onClicked: () => {
                let pt = new PackageTool();
                pt.put_string("echo");
                pt.put_uint32(2);
                WsClient2.send(pt);
            },
            onDraw: () => {
                this.drawButton(this.btn_ros2, null, this.env_.ros2);
            }
        })
        this.btn_dora = new XButton({
            name: "Dora",
            x: 500,
            y: Scr.logich / 2 + 50,
            w: 100,
            h: 100,
            onClicked: () => {
                let pt = new PackageTool();
                pt.put_string("echo");
                pt.put_uint32(3);
                WsClient2.send(pt);
            },
            onDraw: () => {
                this.drawButton(this.btn_dora, null, this.env_.dora);
            }
        })
        this.addUI(this.btn_ros1);
        this.addUI(this.btn_ros2);
        this.addUI(this.btn_dora);
    }
    drawButton(btn, cut, enable) {
        let scale = 1;
        let color = 0xffe0e0e0;
        if (!enable) {
            color = 0xff808080;
        }
        if (btn.isOn()) {
            scale = 1.05;
            color = 0xffffffff;
        }
        if (btn.isDown()) {
            scale = 0.95;
            color = 0xffc0c0c0;
        }
        // cut.draw(btn.centerX(), btn.centerY(), scale, scale, 0, color);
        X2DBeta.ring(btn.centerX(), btn.centerY(), 100, 100, 0, 360, 0xffffffff, 1);
        X2DBeta.drawText(btn.name_, 24, btn.centerX(), btn.bottomY() + 20, 1, 1, 0, -2, -2, 0xffffffff);
        X2DBeta.drawText(enable ? "(已安装)" : "(未安装)", 18, btn.centerX(), btn.bottomY() + 40, 1, 1, 0, -2, -2, enable ? 0xff00ff00 : 0xffff0000);
    }
    onDraw() {
        let btnw = 130;
        let btnh = 130;
        // gap+btnw+gap+btnw+gap+btnw+gap=Scr.logicw
        // gap*4+btnw*3=Scr.logicw
        let gap = (Scr.logicw - btnw * 3) / 4;
        this.btn_ros1.move(gap, Scr.logich / 2, btnw, btnh);
        this.btn_ros2.move(gap * 2 + btnw, Scr.logich / 2, btnw, btnh);
        this.btn_dora.move(gap * 3 + btnw * 2, Scr.logich / 2, btnw, btnh);

        super.onDraw();
        XMultlineText.draw(this.env_.hostDesc, 14, 600, Scr.logicw / 2, Scr.logich / 4, 1, 1, 0, -2, -2, 0xff00ff00);

        if (XTools.AUTO_LOGIN && this.env_.ros1) {
            this.btn_ros1.clickCallback_();
        }
    }
    onTouch(msg, x, y) {
        if (super.onTouch(msg, x, y)) {
            return true;
        }
        return false;
    }
    onKey(type, k) {
        if (type == 0) {
            return true;
        }
        return false;
    }
    static open() {
        XStat.pushStat(new Page2SelectEnv());
    }
}

module.exports = {
    Page2SelectEnv
}